using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace SqlToolkit.Oracle.Schema
{
    public class SqlDbConstraint : SqlBase
    {
        internal SqlDbConstraint() { }
        internal SqlDbConstraint(OracleDB conn) { _conn = conn; }
        public static readonly string CheckConstraint = "C";
        public static readonly string ReadOnlyConstraint = "O";
        public static readonly string PrimaryKey = "P";
        public static readonly string ForeignKey = "R";
        public static readonly string UnuiqueKey = "U";
        public static readonly string ViewCheckOption = "V";

        #region CONSTRAINT_NAME
        protected string constraint_name = string.Empty;
        public string ConstraintName
        {
            get { return constraint_name; }
            //set{if(value != constraint_name) constraint_name = value;}
        }
        #endregion
        #region CONSTRAINT_TYPE \ HOW TELL ME AM PK or FK or ....
        // C - Check on table
        // O - Read only on view
        // P - Primary Key
        // R - Foreign Key
        // U - Unique Key
        // V - Check Option on View
        protected string constraint_type = string.Empty;
        public string ConstraintType
        {
            get { return constraint_type; }
            //set{if(value != constraint_type) constraint_type = value;}
        }
        #endregion
        #region TABLE_NAME
        protected string table_name = string.Empty;
        public string TableName
        {
            get { return table_name; }
            //set{if(value != table_name) table_name = value;}
        }
        #endregion
        #region SEARCH_CONDITION
        protected string search_condition = string.Empty;
        public string SearchCondition
        {
            get { return search_condition; }
            //set{if(value != search_condition) search_condition = value;}
        }
        #endregion

        internal static SqlDbConstraint Build(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbConstraint ret = new SqlDbConstraint(conn);

            ret.constraint_name     = (r.IsDBNull(0)) ? string.Empty : r.GetString(0);
            ret.constraint_type     = (r.IsDBNull(1)) ? string.Empty : r.GetString(1);
            ret.table_name          = (r.IsDBNull(2)) ? string.Empty : r.GetString(2);
            ret.search_condition    = (r.IsDBNull(3)) ? string.Empty : r.GetString(3);

            return ret;
        }
        internal static SqlDbConstraintCollection BuildCollection(OracleDB conn, System.Data.IDataReader r)
        {
            SqlDbConstraintCollection ret = new SqlDbConstraintCollection();

            while(r.Read())
                ret.Add(SqlDbConstraint.Build(conn, r));

            return ret;
        }

        public bool IsPrimaryKey
        {
            get
            {
                if (this.ConstraintType == PrimaryKey)
                    return true;
                else
                    return false;
            }
        }

        public bool IsForeignKey
        {
            get
            {
                if (this.ConstraintType == ForeignKey)
                    return true;
                else
                    return false;
            }
        }

        string constraint_column = string.Empty;
        public string ConstraintColumn
        {
            get
            {
                if (constraint_column == string.Empty)
                    constraint_column = (string)_conn.DbAccessor.ExecuteSqlText.ExecuteScalar("SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = '" + this.constraint_name + "'");

                return constraint_column;
            }
        }

        public string ConstraintColumnFriendly()
        {
            return FriendlyName(this.ConstraintColumn);
        }
    }
}
